app.controller('payCtr',["$scope","$rootScope","ngDialog","$stateParams","payService",function($scope,$rootScope,ngDialog,$stateParams,payService) {
    $scope.data = $stateParams;
    $scope.payType="";
    $scope.payOption = "1";
    // var orderNum = $stateParams.orderNum;
    //
    // $rootScope.loading = false;
    //
    // $scope.price = $stateParams.price;
    // $scope.text = '';
    // $scope.payOption = '';
    //
    //


    // appId : "wxa66a636535c987db",     //公众号名称，由商户传入
    //     timeStamp: datas.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
    //     nonceStr: datas.nonceStr, // 支付签名随机串，不长于 32 位
    //     package: 'prepay_id=' + datas.prepay_id, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
    //     signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
    //     paySign: datas.paySign // 支付签名

    function onBridgeReady(data){
        WeixinJSBridge.invoke('getBrandWCPayRequest',data,function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" || res.err_msg == "get_brand_wcpay_request:cancel" ) {  //如果付款成功或者取消付款都去订单详情页面
                    location.href = $rootScope.prefix + 'order/myOrder/myOrderList.html?status=1';
                }else{
                    alert('发生未知错误');
                }
            }
        );
    }

    function bindEvent(data){
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                }, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                });
                document.attachEvent('onWeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                });
            }
        }else{
            onBridgeReady(data);
        }
    }

    $scope.submit = function(){
        if($scope.payOption ==0){
            $scope.payType="BALANCE";
        }else if($scope.payOption ==1){
            $scope.payType="WEIXIN";
        }
        ngDialog.open({
            template: '/view/order/payYanzhengmaDialog.html',
            width: 400,
            controller: 'payYanzhengmaCtr',
            resolve: {
                getData: function() {
                    return {
                        orderNo : $scope.data.orderNo,
                        payType : $scope.payType
                    };
                }
            }
        });
        // payService.pay({
        //     orderNo : $scope.data.orderNo,
        //     payType : $scope.payType
        // },function(data){
        //     bindEvent(data.data)
        // });
    };
}]);
app.controller('payYanzhengmaCtr',["$scope","$rootScope","ngDialog","$stateParams","getData","payService",function($scope,$rootScope,ngDialog,$stateParams,getData,payService) {
    $scope.data = $stateParams;
    $scope.payType=getData.payType;
    $scope.orderNo=getData.orderNo;
    $scope.verifycode="",
    $scope.payOption = '';
    function onBridgeReady(data){
        WeixinJSBridge.invoke('getBrandWCPayRequest',data,function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" || res.err_msg == "get_brand_wcpay_request:cancel" ) {  //如果付款成功或者取消付款都去订单详情页面
                    location.href = $rootScope.prefix + 'order/myOrder/myOrderList.html?status=1';
                }else{
                    alert('发生未知错误');
                }
            }
        );
    }

    function bindEvent(data){
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                }, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                });
                document.attachEvent('onWeixinJSBridgeReady', function(){
                    onBridgeReady(data)
                });
            }
        }else{
            onBridgeReady(data);
        }
    }
    $scope.goReset = function(){
        location.href = $rootScope.prefix + 'userContent/setUp/setInfo.html';
    };
    $scope.getMa = function(){
        payService.getMa(function(data){
             $scope.verifycode=data.data;  
        });
    };
    $scope.submit = function(){
        payService.pay({
            orderNo : $scope.orderNo,
            payType : $scope.payType,
            verifycode:$scope.verifycode
        },function(data){
            if($scope.payType=="BALANCE"){
                //用账号余额支付
                location.href = $rootScope.prefix + 'order/myOrder/myOrderList.html?status=1';
            }else{
                bindEvent(data.data)  
            }
            ngDialog.closeAll();    
        });
    };
    $scope.cancel = function(){
        ngDialog.closeAll();    
    };
}]);